#include "stm32f10x.h"   // Device header
#include "Delay.h"    // 时间函数
#include "OLED.h"   
#include "LED.h" 
#include "RTC_Control.h" 

//问题:1.设置时间，每次都要写1ong
//2.展示的时间，也是一个1ong
//3.Reset后，或者掉电后，时间又恢复成了初始输入的值
    //参考16.1文档，系统复位或从待机模式唤醒，RTC时钟保持不变
		//处于后备区，当VDD掉电后，RTC还能继续维持
//4.设置一个闹钟

int main(void){
	
	OLED_Init();
	RTC_Control_Config();
	//设置闹钟
	setAlarmConfig(RTC_GetCounter()+10);
	OLED_ShowString(1,1,"    -  -  ");//2025-01-01
	OLED_ShowString(2,1,"  :  :  ");   //12:20:02
  
	while(1){
		time_t now = RTC_GetCounter() +8*3600; //时间戳
		struct tm *timeinfo = localtime(&now); //时间戳转化为tm
		
		OLED_ShowNum(1,1,timeinfo->tm_year + 1900,4);
		OLED_ShowNum(1,6,timeinfo->tm_mon + 1,2);
		OLED_ShowNum(1,9,timeinfo->tm_mday,2);
		OLED_ShowNum(2,1,timeinfo->tm_hour,2);
		OLED_ShowNum(2,4,timeinfo->tm_min,2);
		OLED_ShowNum(2,7,timeinfo->tm_sec,2);
		OLED_ShowNum(3,1,timeinfo->tm_hour,2);
		OLED_ShowNum(3,4,timeinfo->tm_min,2);
		OLED_ShowNum(3,7,timeinfo->tm_sec,2);
		
		//OLED_ShowNum(3,1,RTC_GetCounter(),10); //计数器的值
		
	}
}

void RTC_IRQHandler(void)
{
	if(RTC_GetITStatus(RTC_IT_ALR) == SET)
	{
		OLED_ShowString(4,1,"Alarm");
		Delay_ms(1000);
		OLED_ShowString(4,1,"     ");
		setAlarmConfig(RTC_GetCounter()+10);
		RTC_ClearITPendingBit(RTC_IT_ALR);
	}
	
}
